home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch9 / Robot.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-05-28  |  10.0 KB  |  327 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "Robot"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. ' Control point locations.
  17. Public Cx As Integer    ' Location of top of head.
  18. Public Cy As Integer
  19. Public LShoulderAngle As Single
  20. Public RShoulderAngle As Single
  21. Public LElbowAngle As Single
  22. Public RElbowAngle As Single
  23. Public LHipAngle As Single
  24. Public RHipAngle As Single
  25. Public LKneeAngle As Single
  26. Public RKneeAngle As Single
  27.  
  28. ' Dimensions.
  29. Public UpperArmLength As Single
  30. Public LowerArmLength As Single
  31. Public UpperLegLength As Single
  32. Public LowerLegLength As Single
  33. Public NeckLength As Single
  34. Public TrunkLength As Single
  35. Public BodyLength As Single
  36. Public HeadRadius As Single
  37.  
  38. ' Robot parts.
  39. Public Enum RobotPartEnum
  40.     part_Head
  41.     part_Lelbow
  42.     part_RElbow
  43.     part_LHand
  44.     part_RHand
  45.     part_LKnee
  46.     part_RKnee
  47.     part_LFoot
  48.     part_RFoot
  49.     part_Hips
  50.     part_Neck
  51.     part_Shoulders
  52.     part_MinPart = part_Head
  53.     part_MaxControlPart = part_RFoot
  54.     part_MaxPart = part_Shoulders
  55. End Enum
  56.  
  57. ' Grab handle distances.
  58. Public Enum GrabDistances
  59.     Near = 2
  60.     Near2 = 2 * Near
  61. End Enum
  62. ' Copy another robot's parameters.
  63. Public Sub CopyFrame(from_me As Robot)
  64.     With from_me
  65.         Cx = .Cx
  66.         Cy = .Cy
  67.         LShoulderAngle = .LShoulderAngle
  68.         RShoulderAngle = .RShoulderAngle
  69.         LElbowAngle = .LElbowAngle
  70.         RElbowAngle = .RElbowAngle
  71.         LHipAngle = .LHipAngle
  72.         RHipAngle = .RHipAngle
  73.         LKneeAngle = .LKneeAngle
  74.         RKneeAngle = .RKneeAngle
  75.     End With
  76. End Sub
  77. ' Return the position of part of the robot.
  78. Public Sub Position(part As Integer, x As Integer, y As Integer)
  79.     Select Case part
  80.         Case part_Head
  81.             x = Cx
  82.             y = Cy
  83.         Case part_Neck
  84.             x = Cx
  85.             y = Cy + 2 * HeadRadius
  86.         Case part_Shoulders
  87.             x = Cx
  88.             y = Cy + 2 * HeadRadius + NeckLength
  89.         Case part_Lelbow
  90.             x = Cx + _
  91.                 UpperArmLength * Cos(LShoulderAngle)
  92.             y = Cy + 2 * HeadRadius + NeckLength - _
  93.                 UpperArmLength * Sin(LShoulderAngle)
  94.         Case part_RElbow
  95.             x = Cx + _
  96.                 UpperArmLength * Cos(RShoulderAngle)
  97.             y = Cy + 2 * HeadRadius + NeckLength - _
  98.                 UpperArmLength * Sin(RShoulderAngle)
  99.         Case part_LHand
  100.             x = Cx + _
  101.                 UpperArmLength * Cos(LShoulderAngle) + _
  102.                 LowerArmLength * Cos(LElbowAngle)
  103.             y = Cy + 2 * HeadRadius + NeckLength - _
  104.                 UpperArmLength * Sin(LShoulderAngle) - _
  105.                 LowerArmLength * Sin(LElbowAngle)
  106.         Case part_RHand
  107.             x = Cx + _
  108.                 UpperArmLength * Cos(RShoulderAngle) + _
  109.                 LowerArmLength * Cos(RElbowAngle)
  110.             y = Cy + 2 * HeadRadius + NeckLength - _
  111.                 UpperArmLength * Sin(RShoulderAngle) - _
  112.                 LowerArmLength * Sin(RElbowAngle)
  113.         Case part_Hips
  114.             x = Cx
  115.             y = Cy + 2 * HeadRadius + BodyLength
  116.         Case part_LKnee
  117.             x = Cx + _
  118.                 UpperLegLength * Cos(LHipAngle)
  119.             y = Cy + 2 * HeadRadius + BodyLength - _
  120.                 UpperLegLength * Sin(LHipAngle)
  121.         Case part_RKnee
  122.             x = Cx + _
  123.                 UpperLegLength * Cos(RHipAngle)
  124.             y = Cy + 2 * HeadRadius + BodyLength - _
  125.                 UpperLegLength * Sin(RHipAngle)
  126.         Case part_LFoot
  127.             x = Cx + _
  128.                 UpperLegLength * Cos(LHipAngle) + _
  129.                 LowerLegLength * Cos(LKneeAngle)
  130.             y = Cy + 2 * HeadRadius + BodyLength - _
  131.                 UpperLegLength * Sin(LHipAngle) - _
  132.                 LowerLegLength * Sin(LKneeAngle)
  133.         Case part_RFoot
  134.             x = Cx + _
  135.                 UpperLegLength * Cos(RHipAngle) + _
  136.                 LowerLegLength * Cos(RKneeAngle)
  137.             y = Cy + 2 * HeadRadius + BodyLength - _
  138.                 UpperLegLength * Sin(RHipAngle) - _
  139.                 LowerLegLength * Sin(RKneeAngle)
  140.     End Select
  141. End Sub
  142.  
  143.  
  144.  
  145. ' Draw the robot.
  146. Public Sub Draw(pic As PictureBox, handles As Boolean)
  147. Dim x1 As Integer
  148. Dim y1 As Integer
  149. Dim x2 As Integer
  150. Dim y2 As Integer
  151. Dim x3 As Integer
  152. Dim y3 As Integer
  153.  
  154.     ' Draw the head.
  155.     x1 = Cx
  156.     y1 = Cy + HeadRadius
  157.     pic.Circle (x1, y1), HeadRadius
  158.     If handles Then _
  159.         pic.Line (Cx - Near, Cy - Near)- _
  160.                 Step(Near2, Near2), , BF
  161.  
  162.     ' Draw the body.
  163.     y1 = y1 + HeadRadius
  164.     pic.Line (x1, y1)-Step(0, BodyLength)
  165.     
  166.     ' Draw the left arm.
  167.     y1 = y1 + NeckLength
  168.     x2 = x1 + UpperArmLength * Cos(LShoulderAngle)
  169.     y2 = y1 - UpperArmLength * Sin(LShoulderAngle)
  170.     pic.Line (x1, y1)-(x2, y2)
  171.     x3 = x2 + LowerArmLength * Cos(LElbowAngle)
  172.     y3 = y2 - LowerArmLength * Sin(LElbowAngle)
  173.     pic.Line -(x3, y3)
  174.     If handles Then _
  175.         pic.Line (x2 - Near, y2 - Near)- _
  176.                 Step(Near2, Near2), , BF
  177.     If handles Then _
  178.         pic.Line (x3 - Near, y3 - Near)- _
  179.                 Step(Near2, Near2), , BF
  180.  
  181.     ' Draw the right arm.
  182.     x2 = x1 + UpperArmLength * Cos(RShoulderAngle)
  183.     y2 = y1 - UpperArmLength * Sin(RShoulderAngle)
  184.     pic.Line (x1, y1)-(x2, y2)
  185.     x3 = x2 + LowerArmLength * Cos(RElbowAngle)
  186.     y3 = y2 - LowerArmLength * Sin(RElbowAngle)
  187.     pic.Line -(x3, y3)
  188.     If handles Then _
  189.         pic.Line (x2 - Near, y2 - Near)- _
  190.                 Step(Near2, Near2), , BF
  191.     If handles Then _
  192.         pic.Line (x3 - Near, y3 - Near)- _
  193.                 Step(Near2, Near2), , BF
  194.  
  195.     ' Draw the left leg.
  196.     y1 = y1 + TrunkLength
  197.     x2 = x1 + UpperLegLength * Cos(LHipAngle)
  198.     y2 = y1 - UpperLegLength * Sin(LHipAngle)
  199.     pic.Line (x1, y1)-(x2, y2)
  200.     x3 = x2 + LowerLegLength * Cos(LKneeAngle)
  201.     y3 = y2 - LowerLegLength * Sin(LKneeAngle)
  202.     pic.Line -(x3, y3)
  203.     If handles Then _
  204.         pic.Line (x2 - Near, y2 - Near)- _
  205.                 Step(Near2, Near2), , BF
  206.     If handles Then _
  207.         pic.Line (x3 - Near, y3 - Near)- _
  208.                 Step(Near2, Near2), , BF
  209.  
  210.     ' Draw the right leg.
  211.     x2 = x1 + UpperLegLength * Cos(RHipAngle)
  212.     y2 = y1 - UpperLegLength * Sin(RHipAngle)
  213.     pic.Line (x1, y1)-(x2, y2)
  214.     x3 = x2 + LowerLegLength * Cos(RKneeAngle)
  215.     y3 = y2 - LowerLegLength * Sin(RKneeAngle)
  216.     pic.Line -(x3, y3)
  217.     If handles Then _
  218.         pic.Line (x2 - Near, y2 - Near)- _
  219.                 Step(Near2, Near2), , BF
  220.     If handles Then _
  221.         pic.Line (x3 - Near, y3 - Near)- _
  222.                 Step(Near2, Near2), , BF
  223. End Sub
  224.  
  225.  
  226.  
  227. ' Move the control point to this location.
  228. Public Sub MoveControlPoint(part As Integer, Ax As Integer, Ay As Integer, x As Integer, y As Integer)
  229.     Select Case part
  230.         Case part_Head
  231.             Cx = x
  232.             Cy = y
  233.         Case part_Lelbow
  234.             LShoulderAngle = Arctan2(x - Ax, Ay - y)
  235.         Case part_RElbow
  236.             RShoulderAngle = Arctan2(x - Ax, Ay - y)
  237.         Case part_LHand
  238.             LElbowAngle = Arctan2(x - Ax, Ay - y)
  239.         Case part_RHand
  240.             RElbowAngle = Arctan2(x - Ax, Ay - y)
  241.         Case part_LKnee
  242.             LHipAngle = Arctan2(x - Ax, Ay - y)
  243.         Case part_RKnee
  244.             RHipAngle = Arctan2(x - Ax, Ay - y)
  245.         Case part_LFoot
  246.             LKneeAngle = Arctan2(x - Ax, Ay - y)
  247.         Case part_RFoot
  248.             RKneeAngle = Arctan2(x - Ax, Ay - y)
  249.     End Select
  250. End Sub
  251.  
  252. ' Initialize the robot's parameters.
  253. Public Sub SetParameters(x As Integer, y As Integer, ls As Single, rs As Single, le As Single, re As Single, lh As Single, rh As Single, lk As Single, rk As Single)
  254. Const PI = 3.14159265
  255. Const DEG_TO_RAD = PI / 180#
  256.  
  257.     Cx = x
  258.     Cy = y
  259.     LShoulderAngle = ls * DEG_TO_RAD
  260.     RShoulderAngle = rs * DEG_TO_RAD
  261.     LElbowAngle = le * DEG_TO_RAD
  262.     RElbowAngle = re * DEG_TO_RAD
  263.     LHipAngle = lh * DEG_TO_RAD
  264.     RHipAngle = rh * DEG_TO_RAD
  265.     LKneeAngle = lk * DEG_TO_RAD
  266.     RKneeAngle = rk * DEG_TO_RAD
  267. End Sub
  268.  
  269. ' Return the distance between the top of the head
  270. ' and the top of the robot when its height is as
  271. ' large as possible.
  272. Property Get HeadRoom()
  273.     HeadRoom = (UpperArmLength + LowerArmLength) - _
  274.         (2 * HeadRadius + NeckLength)
  275. End Property
  276. ' Return the maximum possible height the robot
  277. ' can have.
  278. Property Get MaxHeight()
  279.     MaxHeight = TrunkLength + UpperArmLength + LowerArmLength + _
  280.                 UpperLegLength + LowerLegLength
  281. End Property
  282.  
  283. ' Return the maximum possible width the robot
  284. ' can have.
  285. Property Get MaxWidth()
  286.     MaxWidth = 2 * (UpperArmLength + LowerArmLength)
  287. End Property
  288.  
  289.  
  290. ' Read the configuration data from a file.
  291. Sub FileInput(fnum)
  292.     Input #fnum, Cx, Cy, _
  293.         LShoulderAngle, RShoulderAngle, _
  294.         LElbowAngle, RElbowAngle, _
  295.         LHipAngle, RHipAngle, _
  296.         LKneeAngle, RKneeAngle, _
  297.         UpperArmLength, LowerArmLength, _
  298.         UpperLegLength, LowerLegLength, _
  299.         NeckLength, TrunkLength, _
  300.         BodyLength, HeadRadius
  301. End Sub
  302. ' Write the configuration data to a file.
  303. Sub FileWrite(fnum)
  304.     Write #fnum, Cx, Cy, _
  305.         LShoulderAngle, RShoulderAngle, _
  306.         LElbowAngle, RElbowAngle, _
  307.         LHipAngle, RHipAngle, _
  308.         LKneeAngle, RKneeAngle, _
  309.         UpperArmLength, LowerArmLength, _
  310.         UpperLegLength, LowerLegLength, _
  311.         NeckLength, TrunkLength, _
  312.         BodyLength, HeadRadius
  313. End Sub
  314. ' Set default dimensions.
  315. Private Sub Class_Initialize()
  316.     UpperArmLength = 40
  317.     LowerArmLength = 40
  318.     UpperLegLength = 40
  319.     LowerLegLength = 40
  320.     NeckLength = 10
  321.     TrunkLength = UpperArmLength * 1.1
  322.     BodyLength = NeckLength + TrunkLength
  323.     HeadRadius = (UpperArmLength - NeckLength) / 2
  324. End Sub
  325.  
  326.  
  327.